﻿//-----------------------------------------------------------------------
// <copyright file="DeixeiMembershipProvider.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//--------------------------------------------------------------------
namespace Deixei.Web
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.Http;
    using System.Web.Http.Dispatcher;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    using Deixei;
    using Deixei.Domain;
    using Deixei.Domain.Repository;
    using Deixei.Domain.Service;
    using Deixei.Infrastructure;
    using Deixei.Web.Controllers;
    using Deixei.Web.Filters;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;

    public class DeixeiHttpApplication : HttpApplication
    {
        public Assembly CurrentAssembly = Assembly.GetExecutingAssembly();


        //public static IUnityContainer Container;

        public DeixeiWebContext DeixeiWebContext
        {
            get;
            set;
        }



        public void Initialise(bool IsWebApi = false)
        {
            HtmlHelper.ClientValidationEnabled = true;
            HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
            
            if (!IsWebApi)
            { 
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                AuthConfig.RegisterAuth();
            }
            WebApiConfig.Register(GlobalConfiguration.Configuration);

            System.Data.Entity.Database.SetInitializer<DataContext>(new DataContextInitializer());
            DeixeiContainer.Registration();
            DomainUnityRegistration.Registration();

            DeixeiContainer.Container.RegisterType<DeixeiViewMasterPage>();
            DeixeiContainer.Container.RegisterType<DeixeiViewPage>();
            DeixeiContainer.Container.RegisterType<DeixeiViewUserControl>();
            DeixeiContainer.Container.RegisterType<IViewPageActivator, DeixeiViewPageActivator>(new InjectionConstructor(DeixeiContainer.Container));
            DeixeiContainer.Container.RegisterInstance<IHttpControllerActivator>(new HttpControllerActivator(DeixeiContainer.Container));
            DeixeiContainer.Container.RegisterInstance<IFilterProvider>("FilterProvider", new FilterProvider(DeixeiContainer.Container));
            DeixeiContainer.Container.RegisterInstance<IActionFilter>("LogActionFilter", new TraceActionFilter());

            //Container.RegisterInstance<ActionLogRepository>(new ActionLogRepository(Container.Resolve<UnitOfWork>()));

            DeixeiContainer.Container.RegisterInstance<IServiceLocator>(DeixeiContainer.Locator);

            IDependencyResolver resolver = DependencyResolver.Current;
            IDependencyResolver newResolver = new UnityDependencyResolver(DeixeiContainer.Container, resolver);
            DependencyResolver.SetResolver(newResolver);

            NavigationService x = DeixeiContainer.Locator.GetInstance<NavigationService>();
            var y = x.GetWebAppPortfolio();

        }
      
        protected void Application_EndRequest(Object sender, EventArgs e)
        {
            using ( DependencyResolver.Current as IDisposable );
        }

    }
}
